﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmTongHopThanhToanVoiNguoiMua : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private KhachHangBOList khachHangBOList = null;
        private DateTime tuNgay = DateTime.Now;
        private DateTime denNgay = DateTime.Now;
        private string khachHang = "";
        private string tkdu = "";
        #endregion variant

        //=====================================
        #region form
        public FrmTongHopThanhToanVoiNguoiMua()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmTongHopThanhToanVoiNguoiMua_Load);
            this.Shown += new EventHandler(FrmTongHopThanhToanVoiNguoiMua_Shown);
            this.KeyDown += new KeyEventHandler(FrmTongHopThanhToanVoiNguoiMua_KeyDown);

            this.btnSearch.Click += new EventHandler(btnSearch_Click);
            this.btnPrint.Click += new EventHandler(btnPrint_Click);
            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.bandView.RowCellStyle += new RowCellStyleEventHandler(bandView_RowCellStyle);

            this.dtTuNgay.EditValueChanged += new EventHandler(dtTuNgay_EditValueChanged);
            this.chkTKDU.CheckedChanged += new EventHandler(chkTKDU_CheckedChanged);
        }

        private void FrmTongHopThanhToanVoiNguoiMua_Shown(object sender, EventArgs e)
        {
            this.dtTuNgay.Focus();
        }

        private void FrmTongHopThanhToanVoiNguoiMua_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else
                if (this.btnRefresh.Visible && ((e.KeyCode == Keys.R && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F5)))
                    RefreshData();
                else
                    if (this.btnPrint.Visible && ((e.KeyCode == Keys.P && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F7)))
                        Print();
                    else
                        if ((e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F3))
                            FilterData();
        }

        private void FrmTongHopThanhToanVoiNguoiMua_Load(object sender, EventArgs e)
        {
            this.lookUpTKDU.Enabled = false;

            DateTime dtNow = DateTime.Now;
            this.dtTuNgay.DateTime = new DateTime(dtNow.Year, dtNow.Month, 1);
            this.dtDenNgay.DateTime = dtNow;

            Utility.InitControl(this.lookUpKhachHangID);
            Utility.InitControl(this.lookUpTKDU, "StrSoHieu", "StrSoHieu");
            MyStyle.SetGridStyle(this.bandView, false);

            RefreshData();
        }
        #endregion form

        //=====================================
        #region control
        private void dtTuNgay_EditValueChanged(object sender, EventArgs e)
        {
            if (this.dtTuNgay.EditValue != null)
                if ((this.dtTuNgay.EditValue is DBNull) == false)
                {
                    DateTime dt = (DateTime)this.dtTuNgay.EditValue;
                    dt = new DateTime(dt.Year, dt.Month, 1);
                    this.dtTuNgay.EditValue = dt;
                }
        }

        private void chkTKDU_CheckedChanged(object sender, EventArgs e)
        {
            this.lookUpTKDU.Enabled = this.chkTKDU.Checked;
        }
        #endregion control

        //=====================================
        #region button
        private void btnSearch_Click(object sender, EventArgs e)
        {
            FilterData();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Print();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
            FilterData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        private void bandView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            int flag = StaticUtil.GetInt(this.bandView.GetRowCellValue(e.RowHandle, "Flag"), 0);

            if (flag != 0)
            {
                e.Appearance.Font = new Font("Tahoma", 8.25F, FontStyle.Bold);
                e.Appearance.ForeColor = Color.Blue;

                if (e.Column.FieldName == "DienGiai")
                    e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            }
        }
        #endregion grid

        //=====================================
        #region function
        private void FilterData()
        {
            #region validate
            this.dxErrorProvider1.SetError(this.dtTuNgay, null);
            this.dxErrorProvider1.SetError(this.dtDenNgay, null);
            this.dxErrorProvider1.SetError(this.lookUpKhachHangID, null);

            int iTuNgay = this.dtTuNgay.DateTime.Year * 10000 + this.dtTuNgay.DateTime.Month * 100;
            int iDenNgay = this.dtDenNgay.DateTime.Year * 10000 + this.dtDenNgay.DateTime.Month * 100;

            if ((iTuNgay + this.dtTuNgay.DateTime.Day) > (iDenNgay + this.dtDenNgay.DateTime.Day))
            {
                this.dxErrorProvider1.SetError(this.dtTuNgay, "Ngày bắt đầu phải nhỏ hơn hoặc bằng ngày kết thúc");
                this.dxErrorProvider1.SetError(this.dtDenNgay, "Ngày kết thúc phải lớn hơn hoặc bằng ngày bắt đầu");
                return;
            }

            int khachHangID = StaticUtil.GetInt(this.lookUpKhachHangID.EditValue, -1);
            if (khachHangID == -1)
            {
                this.dxErrorProvider1.SetError(this.lookUpKhachHangID, "Chưa chọn khách hàng hoặc nhà cung cấp");
                return;
            }
            #endregion validate

            //////////////////////////////
            #region lay dieu kien
            this.tuNgay = this.dtTuNgay.DateTime;
            this.denNgay = this.dtDenNgay.DateTime;
            this.khachHang = this.lookUpKhachHangID.Text;

            if (this.chkTKDU.Checked)
                this.tkdu = StaticUtil.GetString(this.lookUpTKDU.EditValue, null);
            else
                this.tkdu = null;

            if (this.tuNgay > this.denNgay)
            {
                this.tuNgay = this.denNgay;
                this.denNgay = this.dtTuNgay.DateTime;
            }

            string strTuNgay = "CONVERT(datetime,'" + this.tuNgay.ToString("yyyy-MM-dd") + " 00:00:00',120)";
            string strDenNgay = "CONVERT(datetime,'" + this.denNgay.ToString("yyyy-MM-dd") + " 23:59:59',120)";

            string caption = "TỪ " + this.tuNgay.ToString("dd/MM/yyyy") + " ĐẾN " + this.denNgay.ToString("dd/MM/yyyy") + "  -  ";
            if (this.tkdu != null)
                caption += "TKĐƯ: " + this.tkdu + "  -  ";
            caption += this.khachHang.ToUpper();

            this.bandView.ViewCaption = caption;

            #endregion lay dieu kien

            ///////////////////////

            string where = "NgayLapPhieu>=" + strTuNgay + " AND NgayLapPhieu<=" + strDenNgay + " AND KhachHangID=" + khachHangID.ToString() + " AND LaKhachHang=1";
            if (this.tkdu != null)
                where += " AND ((No=N'131' AND Co=N'" + this.tkdu + "') OR (No=N'" + this.tkdu + "' AND Co=N'131'))";
            else
                where += " AND (No=N'131' OR Co=N'131')";

            ThanhToanVoiNguoiMuaBOCollection thanhToanBOCollection = ThanhToanVoiNguoiMuaBOCollection.GetThanhToanVoiNguoiMuaBOCollection(where, "NgayLapPhieu,So");
            SoDuDauKyBO soDuDauKyBO = SoDuDauKyBO.GetRootSoDuDauKyBOByWhere("KhachHangID=" + khachHangID + " AND Nam=" + this.tuNgay.Year);

            double prvNo = 0;
            double prvCo = 0;

            if (soDuDauKyBO != null)
            {
                prvNo = StaticUtil.GetDouble(soDuDauKyBO.GetValue("No" + this.tuNgay.Month), 0);
                prvCo = StaticUtil.GetDouble(soDuDauKyBO.GetValue("Co" + this.tuNgay.Month), 0);
            }

            ThanhToanVoiNguoiMuaBO dauKyBO = ThanhToanVoiNguoiMuaBOCollection.NewThanhToanVoiNguoiMuaBO();
            dauKyBO.STT0 = null;
            dauKyBO.So = null;
            dauKyBO.NgayLapPhieu = null;
            dauKyBO.DienGiai = "SỐ DƯ ĐẦU KỲ";
            dauKyBO.SoDuNo = prvNo;
            dauKyBO.SoDuCo = prvCo;
            dauKyBO.Flag = -1;
            thanhToanBOCollection.Insert(0, dauKyBO);

            int i = 0;
            int length = thanhToanBOCollection.Count;
            double tongNo = 0;
            double tongCo = 0;
            double no = 0;
            double co = 0;

            ThanhToanVoiNguoiMuaBO thanhToanBO = null;
            for (i = 1; i < length; i++)
            {
                thanhToanBO = thanhToanBOCollection[i];
                co = StaticUtil.GetDouble(thanhToanBO.SoTienCo, 0);
                no = StaticUtil.GetDouble(thanhToanBO.SoTienNo, 0);

                prvNo = prvNo - co + no;
                if (prvNo < 0)
                    thanhToanBO.SoDuCo = Math.Abs(prvNo);
                else
                    thanhToanBO.SoDuNo = prvNo;

                tongNo += no;
                tongCo += co;
            }

            ThanhToanVoiNguoiMuaBO cuoiKyBO = ThanhToanVoiNguoiMuaBOCollection.NewThanhToanVoiNguoiMuaBO();
            cuoiKyBO.STT0 = null;
            cuoiKyBO.So = null;
            cuoiKyBO.NgayLapPhieu = null;
            cuoiKyBO.DienGiai = "SỐ DƯ CUỐI KỲ";
            cuoiKyBO.SoTienNo = tongNo;
            cuoiKyBO.SoTienCo = tongCo;
            if (prvNo < 0)
                cuoiKyBO.SoDuCo = Math.Abs(prvNo);
            else
                cuoiKyBO.SoDuNo = prvNo;
            cuoiKyBO.Flag = -1;
            thanhToanBOCollection.Add(cuoiKyBO);

            this.bds.DataSource = thanhToanBOCollection;
            this.bandView.CheckLoaded();
            SetStt();

            this.btnPrint.Visible = (thanhToanBOCollection.Count > 0);
        }

        private void RefreshData()
        {
            this.khachHangBOList = KhachHangBOList.GetKhachHangBOList();
            Utility.SetData(this.lookUpKhachHangID, this.khachHangBOList, this.khachHangBOList.Count);

            if (this.lookUpKhachHangID.EditValue == null)
                if (this.khachHangBOList.Count > 0)
                    this.lookUpKhachHangID.EditValue = this.khachHangBOList[0].Key;

            SoHieuTaiKhoanBOCollection soHieuTaiKhoanBOCollection = SoHieuTaiKhoanBOCollection.GetSoHieuTaiKhoanBOCollection("SoHieu1<>131");
            soHieuTaiKhoanBOCollection.Sort();
            Utility.SetData(this.lookUpTKDU, soHieuTaiKhoanBOCollection, soHieuTaiKhoanBOCollection.Count);
            if (this.lookUpTKDU.EditValue == null)
                if (soHieuTaiKhoanBOCollection.Count > 0)
                    this.lookUpTKDU.EditValue = soHieuTaiKhoanBOCollection[0].StrSoHieu;
        }

        private void Print()
        {
            if (this.bandView.RowCount > 0)
            {
                Report.rpTongHopThanhToanVoiNguoiMua report = new Report.rpTongHopThanhToanVoiNguoiMua(this.tuNgay, this.denNgay, this.khachHang, this.tkdu, (ThanhToanVoiNguoiMuaBOCollection)this.bds.DataSource);
                report.ShowPreviewDialog();
            }
        }

        private void SetStt()
        {
            int stt = 0;
            int length = this.bandView.RowCount;
            int index = 0;
            for (int i = 0; i < length; i++)
            {
                index = this.bandView.GetVisibleRowHandle(i);
                if (index >= 0)
                    if (StaticUtil.GetInt(this.bandView.GetRowCellValue(index, "Flag"), 0) == 0)
                        this.bandView.SetRowCellValue(index, "STT0", ++stt);
            }
        }
        #endregion function
    }
}